package cog.support.mybatis.generator.plugin;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.codegen.ibatis2.Ibatis2FormattingUtilities;

import java.util.List;

/**
 * Mybatis generator 插件
 *
 * @author 陈杰
 * @since 2016年8月23日 10:52:20
 * @version v1.0
 *
 * Copyright ChenJie(chenjie@co-lander.com)
 */
public class MyBatisPaginationPlugin extends PluginAdapter {


    /**
     * 创建Mapper Interface的时候被调用
     * 添加自定义方法
     * */
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass,IntrospectedTable introspectedTable){
//        addInterfaceMethod(interfaze,topLevelClass,introspectedTable);
        return true;
    }


    @Override
    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,IntrospectedTable introspectedTable) {

        InnerClass criteria = null;
        // first, find the Criteria inner class
        for (InnerClass innerClass : topLevelClass.getInnerClasses()) {
            if ("GeneratedCriteria".equals(innerClass.getType().getShortName())) { //$NON-NLS-1$
                criteria = innerClass;
                break;
            }
        }

        if (criteria == null) {
            // can't find the inner class for some reason, bail out.
            return true;
        }

        for (IntrospectedColumn introspectedColumn : introspectedTable
                .getNonBLOBColumns()) {
            if (!introspectedColumn.isJdbcCharacterColumn()
                    || !introspectedColumn.isStringColumn()) {
                continue;
            }

            Method method = new Method();
            method.setVisibility(JavaVisibility.PUBLIC);
            method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), "value")); //$NON-NLS-1$

            StringBuilder sb = new StringBuilder();
            sb.append(introspectedColumn.getJavaProperty());
            sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
            sb.insert(0, "and"); //$NON-NLS-1$
            sb.append("LikeInsensitive"); //$NON-NLS-1$
            method.setName(sb.toString());
            method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());

            sb.setLength(0);
            sb.append("addCriterion(\"upper("); //$NON-NLS-1$
            sb.append(Ibatis2FormattingUtilities
                    .getAliasedActualColumnName(introspectedColumn));
            sb.append(") like\", value.toUpperCase(), \""); //$NON-NLS-1$
            sb.append(introspectedColumn.getJavaProperty());
            sb.append("\");"); //$NON-NLS-1$
            method.addBodyLine(sb.toString());
            method.addBodyLine("return (Criteria) this;"); //$NON-NLS-1$

            criteria.addMethod(method);
        }

        return true;
    }


//    private void addInterfaceMethod(Interface interfaze,TopLevelClass topLevelClass,IntrospectedTable introspectedTable){
//        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
//        Method method = new Method();
//
//
//        FullyQualifiedJavaType methodReturnType = new FullyQualifiedJavaType("java.util.HashMap")
//
//        method.setReturnType(methodReturnType);
//        method.setVisibility(JavaVisibility.PUBLIC);
//        method.setName("selectPageByExample");
//        importedTypes.add(methodReturnType);
//
//        FullyQualifiedJavaType parameterType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
//        FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass();
//        importedTypes.add(parameterType);
//        method.addParameter(new Parameter(parameterType, "record"));
//
//        context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
//
//        interfaze.addImportedTypes(importedTypes);
//        interfaze.addMethod(method);
//    }

    /**
     * This plugin is always valid - no properties are required
     */
    public boolean validate(List<String> warnings) {
        return true;
    }

}
